﻿using System;
using System.Collections.Generic;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using HuaCheng.Model;

namespace HuaCheng.Controller
{
    public class PulseControl
    {
        /// <summary>
        /// 根据TreeList生成脉冲工况列表
        /// </summary>
        /// <param name="treeList"></param>
        /// <returns></returns>
        public static List<PulseStep> GetPulseStepByTreeList(TreeList treeList)
        {
            List<PulseStep> pulseList = new List<PulseStep>();
            //遍历treeList节点
            for (int i = 0; i < treeList.Nodes.Count; i++)
            {
                PulseStep pulseStep = GetPulseStepByTreeListNode(treeList, i);
                pulseList.Add(pulseStep);
            }
            return pulseList;
        }
        /// <summary>
        /// 提取treeList节点为PulseStep对象
        /// </summary>
        /// <param name="treeList"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        private static PulseStep GetPulseStepByTreeListNode(TreeList treeList, int i)
        {
            PulseStep pulseStep = new PulseStep();
            //获取slopeStep的属性
            Type type = pulseStep.GetType();
            //遍历treelist的每一列
            for (int j = 0; j < treeList.Columns.Count; j++)
            {
                string strColumn = string.Empty;
                object copyItem = treeList.GetRowCellValue(treeList.Nodes[i], treeList.Columns[j]);
                if (copyItem != null)
                {
                    strColumn = Convert.ToString(copyItem);
                }
                //给每个属性赋表中对应单元格的值
                type.GetProperty(treeList.Columns[j].FieldName).SetValue(pulseStep, strColumn);
            }
            return pulseStep;
        }
        /// <summary>
        /// 根据脉冲工况列表生成TreeList
        /// </summary>
        /// <param name="stepList"></param>
        /// <param name="treeList"></param>
        /// <returns></returns>
        public static List<PulseModel> SetTreeListByPulseList(List<PulseStep> pulseList)
        {
            List<PulseModel> ds = new List<PulseModel>();
            //遍历工步信息列表
            for (int i = 0; i < pulseList.Count; i++)
            {
                PulseStep pulseStep = pulseList[i];
                SetTreeListNodeByPulseStep(ds,pulseStep,i);
            }
            return ds;
        }
        ///// <summary>
        ///// 根据PulseStep对象生成TreeList节点
        ///// </summary>
        ///// <param name="PulseStep"></param>
        ///// <param name="treeList"></param>
        ///// <param name="i"></param>
        ///// <returns></returns>
        //private static TreeList SetTreeListNodeByPulseStep(PulseStep pulseStep, TreeList treeList, int i)
        //{
        //    //新增表节点
        //    TreeListNode tmpNode = treeList.Nodes.Add();
        //    //获取PulseStep属性
        //    Type type = pulseStep.GetType();
        //    for (int j = 0; j < treeList.Columns.Count; j++)
        //    {
        //        if (type.GetProperty(treeList.Columns[j].FieldName).GetValue(pulseStep) != null)
        //        {
        //            treeList.SetRowCellValue(tmpNode, treeList.Columns[j], Convert.ToString(type.GetProperty(treeList.Columns[j].FieldName).GetValue(pulseStep)));
        //        }
        //    }
        //    treeList.SetNodeIndex(tmpNode, i);
        //    return treeList;
        //}

        /// <summary>
        /// 采用数据源方式生成TreeList节点,提高加载效率
        /// </summary>
        /// <param name="PulseStep"></param>
        /// <param name="treeList"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        private static void SetTreeListNodeByPulseStep(List<PulseModel> ds,PulseStep pulseStep,int i)
        {
            PulseModel pulseModel = new PulseModel();
            pulseModel.ParentID = 0;
            pulseModel.ID = i + 1;
            pulseModel.SerialNum =pulseStep.SerialNum;
            pulseModel.CurrValue = pulseStep.CurrValue;
            pulseModel.Time = pulseStep.Time;
            ds.Add(pulseModel);
        }
    }
}
